package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.ClientRegistrationListener;
import com.acme.bankapp.domain.bank.Gender;

public class BankService {
	public Bank createNewBank() {
		Bank bank = new Bank();
		return bank;
	}

	public void printMaximumAmountToWithdraw(Bank bank)
			throws AccountDoesntExistsException {
		Client[] clients = bank.getClients();
		for (Client i : clients) {
			if (i != null) {
				if (i.getAccount() == null) throw new AccountDoesntExistsException();
				System.out.println("MaximumAmounttoWithdraw for "
						+ i.getClientSalutation() + " is "
						+ i.getAccount().maximumAmountToWithdraw());
			}

		}
	}

	public Client addClient(Bank bank, String name, Gender gender)
			throws ClientExistsException {
		Client[] clients = bank.getClients();
		for (Client i : clients) {
			if (i != null && i.getName() == name)
				throw new ClientExistsException();
		}
		ClientRegistrationListener[] listeners = bank.getListeners();
		for (int i = 0; i < clients.length; i++) {
			if (clients[i] == null) {
				Client client = new Client();
				clients[i] = client;
				clients[i].setName(name);
				clients[i].setGender(gender);
				for (ClientRegistrationListener l : listeners) {
					l.onClientAdded(clients[i]);
				}
				return clients[i];
			}
		}
		return null;
	}

	public void printBalance(Bank bank) throws AccountDoesntExistsException {
		System.out.println();
		Client[] clients = bank.getClients();
		{
			for (Client i : clients) {
				if (i != null) {
					if (i.getAccount() == null) throw new AccountDoesntExistsException();
					System.out.println(i.getClientSalutation()
							+ "'s balance = " + i.getAccount().toString());

				}
			}
			System.out.println();
		}
	}

	public void modifyBank(Bank bank) throws NotEnoughFundsException,
			AccountDoesntExistsException {
		System.out.println();
		Client[] clients = bank.getClients();
		if (clients[0] != null) {
			if (clients[0].getAccount() == null) throw new AccountDoesntExistsException();
			System.out.println("Modifying " + clients[0].getClientSalutation()
					+ "'s account...");
			clients[0].getAccount().withdraw(90000.00);
		}
		if (clients[1] != null) {
			if (clients[1].getAccount() == null) throw new AccountDoesntExistsException();
			System.out.println("Modifying " + clients[1].getClientSalutation()
					+ "'s account...");
			clients[1].getAccount().deposit(150000.00);
		}
		if (clients[2] != null) {
			if (clients[2].getAccount() == null) throw new AccountDoesntExistsException();
			System.out.println("Modifying " + clients[2].getClientSalutation()
					+ "'s account...");
			clients[2].getAccount().withdraw(120000.00);
		}
		if (clients[3] != null) {
			if (clients[3].getAccount() == null) throw new AccountDoesntExistsException();
			System.out.println("Modifying " + clients[3].getClientSalutation()
					+ "'s account...");
			clients[3].getAccount().withdraw(120000.00);
		}
		if (clients[4] != null) {
			if (clients[4].getAccount() == null) throw new AccountDoesntExistsException();
			System.out.println("Modifying " + clients[4].getClientSalutation()
					+ "'s account...");
			clients[4].getAccount().withdraw(120000.00);
		}
		System.out.println("Accounts modification finished");
	}
}
